import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: '/',
    //   name: 'home',
    //   component: HomeView,
    // },redirect
    {
      path: '/',
      redirect: '/home/index',
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
    },
    {
      path: '/deil',
      name: 'deil',
      component: () => import('@/views/deil/index.vue'),
    },
    {
      path: '/reg',
      name: 'reg',
      component: () => import('@/views/reg/index.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/index.vue'),
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import('@/views/home/children/index.vue'),
        },
        {
          path: 'cal',
          name: 'cal',
          component: () => import('@/views/home/children/cal.vue'),
        },
        {
          path: 'shop',
          name: 'shop',
          component: () => import('@/views/home/children/shop.vue'),
        },
        {
          path: 'my',
          name: 'my',
          component: () => import('@/views/home/children/my.vue'),
        },
        {
          path: 'men',
          name: 'men',
          component: () => import('@/views/home/children/men.vue'),
        },
      ]
    },
  ],
})

// GOOD
router.beforeEach((to, from, next) => {
  let isAuthenticated = localStorage.getItem('token')
  if ((to.name == 'shop' || to.name == 'my') && !isAuthenticated) {
    next({ name: 'login' })
  }
  else {
    next()
  }
})

export default router
